package fm.dongman.animefm.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import fm.dongman.animefm.R;

import java.util.List;

import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.view.activity.WebViewActivity;
import fm.dongman.animefm.view.transform.GlideRoundTransform;
import fm.dongman.modellib.model.ArticleModel;

/**
 * 动漫文章适配器 ArticleModel
 * Created by Administrator on 2017/10/10.
 */

public class HomeArticleAdapter extends RecyclerView.Adapter<HomeArticleAdapter.ViewHolder> {

    List<IModel.IArticleModel> mListArticle;
    private Context mContext;

    public HomeArticleAdapter(List<IModel.IArticleModel> list, Context context) {
        this.mListArticle = list;
        this.mContext = context;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView mBrowseCount;   //浏览次数
        private TextView mCreateTime;    //创建时间
        private ImageView mImgUrl;        //图片
        private TextView mSummary;       //简介
        private TextView mTitle;         //标题
        private View mSplitLine;          //列表线
        private View mLatesView;          //列表view

        public ViewHolder(View itemView) {
            super(itemView);
            mBrowseCount = (TextView) itemView.findViewById(R.id.mTvTheLatestArticleBrowseCount);
            mCreateTime = (TextView) itemView.findViewById(R.id.mTvTheLatestAnimeCreateTime);
            mImgUrl = (ImageView) itemView.findViewById(R.id.mIvArticleListCover);
            mSummary = (TextView) itemView.findViewById(R.id.mTvTheLatestAnimeSummary);
            mTitle = (TextView) itemView.findViewById(R.id.mTvTheLatestAnimeTitle);
            mSplitLine = itemView.findViewById(R.id.mSplitLine);
        }
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_the_latest_anime, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final ArticleModel article = (ArticleModel) mListArticle.get(position);
        Glide.with(mContext)
                .load(article.getImgUrl())
                .transform(new CenterCrop(mContext), new GlideRoundTransform(mContext, 2))
                .into(holder.mImgUrl);     //设置网络url
        holder.mSummary.setText(article.getSummary());
        nullValueHandle(holder, article);                                        //null值处理

        holder.mBrowseCount.setText(article.getBrowseCount());
        holder.mTitle.setText(article.getTitle());

        //单击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContext.startActivity(WebViewActivity.getIntent(mContext, article.getArticleURL()));
            }
        });
    }


    /**
     * null值处理
     *
     * @param holder
     * @param article
     */
    public void nullValueHandle(ViewHolder holder, ArticleModel article) {
        if (article.getCreateTime() != null) {
            holder.mCreateTime.setText(article.getCreateTime());
        } else {
            holder.mCreateTime.setText("刚刚");
        }
    }

    @Override
    public int getItemCount() {
        return mListArticle.size();
    }


}
